package presentationTier;

import businessTier.IMovieFacade;
import businessTier.MovieFacade;
import core.Movie;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.table.TableModel;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * Globalni funkce a konstanty, ktere se pouzivaji v presentationTier
 * @author vojta
 */
public class Global {

    /**
     * Hlavicka pro export do XML
     */
    public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \n" +
                      "<!DOCTYPE movies [ \n" +
                      "<!ELEMENT movies    (movie)+> \n" +
                      "<!ELEMENT movie     (id,name,year,director,medium,genre)> \n" +
                      "<!ELEMENT year        (#PCDATA)> \n" +
                      "<!ELEMENT director        (#PCDATA)> \n" +
                      "<!ELEMENT id        (#PCDATA)> \n" +
                      "<!ELEMENT name      (#PCDATA)> \n" +
                      "<!ELEMENT medium      EMPTY> \n" +
                      "<!ATTLIST medium id CDATA #REQUIRED \n" +
                      "                 name CDATA #REQUIRED> \n" +
                      "<!ELEMENT genre EMPTY> \n" +
                      "<!ATTLIST genre id CDATA #REQUIRED \n" +
                      "                name CDATA #REQUIRED> \n" +
                      "]>\n" +
                      "<movies> \n";

    /**
     * Hlavicka pro export do HTML
     */
    public static final String HTML_HEADER = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" +
            "\n<html>\n" +
            "   <head>\n" +
            "       <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> \n" +
            "       <title>Exported data from Movie Database from "  + getDate() + "</title>\n" +
            "       </head>\n" +
            "   <body>\n" +
            "       <table border='1' style='border-collapse: collapse' align='center'>\n" +
            "<tr><th>Id</th><th>Name</th><th>Year</th><th>Director</th><th>Medium id</th>\n" +
            "<th>Medium</th><th>Genre id</th><th>Genre</th></tr>\n";


    private static String getDate() {

        DateFormat dateFormat = new SimpleDateFormat("dd.mm.yyyy");
        Date date = new Date();
        return dateFormat.format(date);
    
    }

    /**
     * Nacte databazi a zobrazi v jTable1
     * @param form
     */
    public static void open(MainForm form) {
        JFileChooser fc = new JFileChooser();
        int ret = fc.showOpenDialog(fc);
        String path = "";
        String file = "";
        String completePath = "";
        if (ret == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile().getName().toString();
            path = fc.getSelectedFile().getParent().toString();
            String[] file1 = file.split("\\.");
            completePath = path + "/" + file1[0];
            //JOptionPane.showMessageDialog(form, completePath);

            try {
                IMovieFacade mf = MovieFacade.createInstance(completePath);
                List<Movie> list = mf.getAllMovies();

                setTableModel(list, form);

            } catch (Exception ex) {
                ex.printStackTrace();
                form.error(ex);
            }

        }
    }

    /**
     * Nacte znovu data z databaze
     * @param form
     */
    public static void refreshTable(MainForm form) {
        try {
                IMovieFacade mf = MovieFacade.getInstance();
                List<Movie> list = mf.getAllMovies();
                
                setTableModel(list,form);

            } catch (Exception ex) {
                ex.printStackTrace();
                form.error(ex);
            }
    }

    /**
     * Vytvori novou databazi a naplni jTable
     * @param form
     */
     public static void create(MainForm form) {
        JFileChooser fc = new JFileChooser();
        int ret = fc.showOpenDialog(fc);
        String path = "";
        String file = "";
        String completePath = "";
        if (ret == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile().getName().toString();
            path = fc.getSelectedFile().getParent().toString();
            String[] file1 = file.split("\\.");
            completePath = path + "/" + file1[0];
            //JOptionPane.showMessageDialog(form, completePath);

            try {
                IMovieFacade mf = MovieFacade.createInstance(completePath);
                mf.createDatabase();
                List<Movie> list = mf.getAllMovies();

                setTableModel(list, form);

                mf.close();
            } catch (Exception ex) {
                ex.printStackTrace();
                form.error(ex);
            }

        }
    }

     /**
     * Nastavi jTable1 jeji TableModel
      * @param list
      * @param form
      */
     public static void setTableModel(List<Movie> list, MainForm form) {
        TableModel tm = new MovieTableModel(list);
        form.jTable1.setModel(tm);
     }

     /**
      * Povoli/zakaze menu
      * @param form
      * @param value
      */
     public static void enableComponents(MainForm form, boolean value) {
        form.jMenu2.setEnabled(value);
        form.jMenu3.setEnabled(value);
        form.jMenu4.setEnabled(value);
        form.jMenu5.setEnabled(value);
        form.jMenu6.setEnabled(value);
     }

}
